package org.wangnian.validationdemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wangnian.validationdemo.model.Book;
import org.wangnian.validationdemo.model.BookList;
import org.wangnian.validationdemo.service.BookService;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/book")
@Validated
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping("/get")
    public String get(String bookId){
        bookService.getBooKById(bookId);
        return "查询成功";
    }

    /**
     * 再 controller 校验实体类中的 list 集合
     */
    @GetMapping("/list")
    public String list(@RequestBody @Validated BookList bookList){
        return "校验 list 集合";
    }

    /**
     * 在 service 校验实体类中的 list 集合
     */
    @GetMapping("/list2")
    public String list2(@RequestBody BookList bookList){
        bookService.list2(bookList);
        return "校验 list2 集合";
    }

    /**
     * 再 controller 校验 list 集合参数
     */
    @GetMapping("/list3")
    public String list3(@RequestBody @Valid List<Book> bookList){
        return "校验 list3 集合";
    }

    /**
     * 在 service 校验 list 集合参数
     */
    @GetMapping("/list4")
    public String list4(@RequestBody List<Book> bookList){
        bookService.list4(bookList);
        return "校验 list4 集合";
    }

}
